package com.cskt.itripauth.common.handler;

import com.cskt.common.constants.ErrorCodeEnum;
import com.cskt.common.exception.DaoException;
import com.cskt.common.exception.ServiceException;
import com.cskt.common.exception.SysException;
import com.cskt.common.vo.ReturnResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @RestControllerAdvice：controller的异常增强
 */

@RestControllerAdvice
public class GlobalExceptionHandler {
    //把当前类的异常做日志输出
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //@ExceptionHandler：如果发生某种异常，进入当前方法
    @ExceptionHandler(value = Exception.class)
    public ReturnResult error(Exception e) {
        LOGGER.error(e.getMessage(),e);
        return ReturnResult.error(ErrorCodeEnum.SYSTEM_EXECUTION_ERROR);
    }

    //用来捕获dao层异常
    @ExceptionHandler(value = DaoException.class)
    public ReturnResult error(DaoException e) {
        LOGGER.error(e.getMessage(),e);
        return ReturnResult.error(e.getErrorCode(),e.getMessage());
    }

    //用来捕获业务逻辑层异常
    @ExceptionHandler(value = ServiceException.class)
    public ReturnResult error(ServiceException e) {
        LOGGER.error(e.getMessage(),e);
        return ReturnResult.error(e.getErrorCode(),e.getMessage());
    }

    //用来捕获系统异常
    @ExceptionHandler(value = SysException.class)
    public ReturnResult error(SysException e) {
        LOGGER.error(e.getMessage(),e);
        return ReturnResult.error(e.getErrorCode(),e.getMessage());
    }
}
